<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="36%"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :show-close="false"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="企业名称" prop="company_name">
          <el-input v-model="ruleForm.company_name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业地址" prop="address">
          <el-input v-model="ruleForm.address" placeholder="请输入企业详细地址" />
        </el-form-item>
        <el-form-item label="营业执照有效期">
          <el-date-picker
            v-model="timeArr"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            style="width: 100%"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="timeConfirm"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业logo" prop="company_logo">
              <uploadImage v-model="ruleForm.company_logo" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="company_cert">
              <uploadImage v-model="ruleForm.company_cert" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm">取消</el-button>
          <el-button type="primary" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { detailCompanyUser, editCompanyUser, createdCompanyUser } from '@/api/companyUser'
import { getToken } from '@/utils/auth'

export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      timeArr: [],
      header: {},
      baseURL: process.env.VUE_APP_BASE_API,
      ruleForm: {
        company_name: '',
        address: '',
        company_logo: '',
        company_cert: '',
        company_start: '',
        company_end: ''
      },
      rules: {
        company_name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入企业详细地址', trigger: 'blur' }
        ],
        company_logo: [
          { required: true, message: '请上传企业logo', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.header['Authorization'] = 'Bearer ' + getToken()
    this.header['Lang'] = 'zh-CN'
    // 编辑
    if (this.id) {
      this.getDetailById(this.id)
    }
  },
  methods: {
    // 获取详情
    getDetailById(id) {
      detailCompanyUser({ id }).then(res => {
        this.ruleForm = res.data
        this.timeArr = [res.data.company_start, res.data.company_end]
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.id) {
            //  是编辑
            editCompanyUser(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createdCompanyUser(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file)
      this.ruleForm.company_logo = res.data.realPath
    },
    handleAvatarSuccess1(res, file) {
      this.ruleForm.company_cert = res.data.realPath
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 时间选择
    timeConfirm(e) {
      this.ruleForm.company_start = e[0]
      this.ruleForm.company_end = e[1]
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 10px;
  border: 1px dashed #cccccc;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 10px;
}
</style>
